<link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.4/bootstrap-editable/css/bootstrap-editable.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.4/bootstrap-editable/js/bootstrap-editable.min.js"></script>

<div id="Studentimodal" class="modal hide">
<form action="<?php echo $this->webroot; ?>Studente/setelem" method="POST" id="studenteForm">
	  <div class="modal-header">
	   <h4> Aggiungi Studente alla Classe:</h4>
	      <label class="muted">saranno visualizzati solo gli studenti NON associati ad altre classi</label>
	   </div>
	  <div class="modal-body">
	<p>
			<input type="hidden" name="Classe[id]" value="<?php echo $classe['Classe']['id']?>">
			<select id="selectStudentiClasse" class="select2 span5" name="Studente[id]">
				<option value="0"></option>
				<?php foreach ($listStudentiFree as $studentefree) {?>
					<option value="<?php echo $studentefree['Studente']['id']?>"><?php echo $studentefree['Studente']['cognome']." ".$studentefree['Studente']['nome']." (".$studentefree['Studente']['data_nascita'].")"; ?></option>
				<?php }?>
			</select>
	</p>
	  </div>
	  <div class="modal-footer">
	<a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Annulla</a>
	<button class="btn btn-primary" type="submit" aria-hidden="true">Ok</button>
	  </div>
	</form>
</div>

<div id="Docentimodal" class="modal hide">
  <form action="<?php echo $this->webroot; ?>Docente/setelem" method="POST" >
  <div class="modal-header">
   <h4> Aggiungi Docente alla Classe:</h4>
   </div>
  <div class="modal-body">
<p>
		<input type="hidden" name="Classe[Classe]" value="<?php echo $classe['Classe']['id']?>">
		<select id="selectDocentiClasse" class="select2 span5" name="Docente[id]">
			<option value="0"></option>
			<?php foreach ($listDocenti as $docente) {?>
				<option value="<?php echo $docente['Docente']['id']?>"><?php echo $docente['User']['Anagrafica']['cognome']." ".$docente['User']['Anagrafica']['nome']. " (".$docente['Ruolo']['denominazione'].")"; ?></option>
			<?php }?>
		</select>
</p>
  </div>
  <div class="modal-footer">
	<a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Annulla</a>
	<button class="btn btn-primary" type="submit" aria-hidden="true">Ok</button>
  </div>
</form>
</div>
		
<div id="Disciplinamodal" class="modal hide">
	<form action="<?php echo $this->webroot; ?>Disciplina/setelem" method="POST">
  <div class="modal-header">
   <h4> Aggiungi Disciplina alla Classe:</h4>
   <label class="muted">saranno visualizzate solo le discipline associate a questo indirizzo di studio</label>
   </div>
  <div class="modal-body">
<p>
		<input type="hidden" name="Classe[Classe]" value="<?php echo $classe['Classe']['id']?>">
		<select id="selectDisciplinaClasse" class="select2 span5" name="Disciplina[id]">
			<option value="0"></option>
			<?php foreach ($listDiscipline as $disciplina) {?>
				<option value="<?php echo $disciplina['id']?>"><?php echo $disciplina['nome']; ?></option>
			<?php }?>
		</select>
</p>
  </div>
  <div class="modal-footer">
	<a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Annulla</a>
	<button class="btn btn-primary" type="submit" aria-hidden="true">Ok</button>
  </div>
</form>
</div>

<ul class="breadcrumb">
  <li><a href="<?php echo $this->webroot;?>Home/index">Home</a> <span class="divider">/</span></li>
  <li><a href="<?php echo $this->webroot;?>Istituto/wizard?tab=tre">Lista Classi</a> <span class="divider">/</span></li>
  <li class="active">Classe</li>
</ul>		
		
<h3 class="demo-panel-title">
	<i class="icon-book"></i> Configurazione Classe <a id="anno" href="#"><?php echo $classe['Classe']['anno']; ?></a><a id="sezione" href="#"><?php echo $classe['Classe']['sezione'];?></a> - <a id="indirizzostudio"></a></small>
</h3>
<div class="navbar navbar-inverse" id="sticky">
	<div class="navbar-inner">
	  <div class="container">
	    <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target="#nav-collapse-01">
	    	<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
	    </button>
	    <div class="nav-collapse collapse" id="nav-collapse-01">
	      <ul class="nav">
	        <li>
				<a><i class="icon-copy"></i> Duplica Classe</a>
	        </li>
	        <li>
				<a onclick="$('#Studentimodal').modal()"><i class="icon-plus-sign-alt"></i> Aggiungi Studente</a>
	        </li>
	        <li>
				<a onclick="$('#Docentimodal').modal()"><i class="icon-plus-sign-alt"></i> Aggiungi Docente</a>
	        </li>
	        <li>
				<a onclick="$('#Disciplinamodal').modal()"><i class="icon-plus-sign-alt"></i> Aggiungi Disciplina</a>
	        </li>
	      </ul>
	    </div><!--/.nav -->
	  </div>
	</div>
</div>
<div class="">
 		<!--LISTA STUDENTI-->
        <h3 class="demo-panel-title">Studenti <span style="top: -4px; position: relative;" class="badge badge-warning"><?php echo count($classe['Studente'])?></span> </h3>
        <div class="">
        <table class="table table-hover">
		<thead>
			<th><?php echo __("Cognome")?></th>	
			<th><?php echo __("Nome")?></th>	
			<th><?php echo __("Sesso")?></th>	
			<th><?php echo __("Data di Nascita")?></th>	
			<th></th>
		</thead>
		<?php if(count($classe['Studente'])==0){?>
			<tr><td colspan="5">Nessuno Studente è stato associato alla classe.</td></tr>
		<?php }?>
		<?php foreach ($classe['Studente'] as $studente) { ?>
		<tr>
			<td><?php echo $studente['User']['Anagrafica']['cognome']; ?></td>	
			<td><?php echo $studente['User']['Anagrafica']['nome']; ?></td>	
			<td><?php echo $studente['User']['Anagrafica']['sesso']; ?></td>	
			<td><?php echo $studente['User']['Anagrafica']['data_nascita']; ?></td>
			<td><a href="<?php echo $this->webroot;?>Studente/remove/<?php echo $studente['id']?>/<?php echo $classe['Classe']['id']?>" ><i class="icon-remove-sign"></i> rimuovi</a></td>	
		</tr>		
		<?php }?>
		</table>
        </div>
 		<!--LISTA DOCENTI-->
        <h3 class="demo-panel-title">Docenti <span style="top: -4px; position: relative;" class="badge badge-warning"><?php echo count($classe["Docente"])?></span></h3>
        <div class="">
        <table class="table table-hover">
		<thead>
			<th><?php echo __("Cognome")?></th>	
			<th><?php echo __("Nome")?></th>	
			<th><?php echo __("Sesso")?></th>	
			<th><?php echo __("Ruolo")?></th>	
			<th></th>	
		</thead>
		<?php if(count($classe["Docente"])==0){?>
			<tr><td colspan="5">Nessun Docente è stato associato alla classe.</td></tr>
		<?php }?>
		<?php foreach ($classe["Docente"] as $docente) { ?>
		<tr>
			<td><?php echo $docente['User']['Anagrafica']['cognome']; ?></td>	
			<td><?php echo $docente['User']['Anagrafica']['nome']; ?></td>	
			<td><?php echo $docente['User']['Anagrafica']['sesso']; ?></td>	
			<td><?php echo $docente['Ruolo']['denominazione']; ?></td>
			<td><a href="<?php echo $this->webroot;?>docente/remove/<?php echo $docente['id']?>/<?php echo $classe['Classe']['id']?>" ><i class="icon-remove-sign"></i> rimuovi</a></td>	

		</tr>		
		<?php }?>
		</table>
        </div>
        <!--LISTA ASSOCIAZIONI-->
        <h3 class="demo-panel-title">Disciplina / Docente <span style="top: -4px; position: relative;" class="badge badge-warning"><?php echo count($classeDisciplinaDocenteList)?></span></h3>
        <div class="">
		<form id="frm" class="form-inline editableform" method="post" action="<?php echo $this->webroot;?>Disciplina/setelem" >
        <table class="table table-hover">
		<thead>
			<th><?php echo __("Disciplina")?></th>	
			<th><?php echo __("Prove")?></th>	
			<th><?php echo __("ore/settimana")?></th>	
			<th><?php echo __("Docente")?></th>	
			<th><?php echo __("Supplente 1")?></th>	
			<th><?php echo __("Supplente 2")?></th>	
			<th></th>	
		</thead>
		<?php if(count($classeDisciplinaDocenteList)==0){?>
		<tr><td colspan="7">Nessuna Disciplina è stata associata alla classe.</td></tr>
		<?php }?>
		<?php foreach ($classeDisciplinaDocenteList as $disciplinaDocente) { ?>
		<tr id="<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['id']; ?>">
			<td><input type="hidden" id="idClasseDisciplina" name="ClasseDisciplinaDocente[id]" value="<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['id']; ?>" />
			<b><?php echo $disciplinaDocente['Disciplina']['nome']; ?></b></td>	
			<td><a href="#" class="optionsProve" data-type="checklist" data-value='<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['prove_lista']; ?>' id="prove"></a></td>	
			<td>
				<div class="spinner">
					<input  id="oreSettimanali" type="number" name="ClasseDisciplinaDocente[ore_settimanali]" pattern="[0-9]*" maxlength="2" value="<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['ore_settimanali']; ?>" class="input-mini spinner-input">
					<!--SPINNER DA MIGLIORARE E METTERE :
						<div class="spinner-buttons	btn-group btn-group-vertical">
						<button type="button" class="btn spinner-up">
							<i class="icon-chevron-up"></i>
						</button>
						<button type="button" class="btn spinner-down">
							<i class="icon-chevron-down"></i>
						</button>
					</div>-->
				</div>
			</td>
			<td><a href='#' id='docenti' class='change_docente' data-type='select2' data-value='<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['docente_id']?>' ></a></td>	
			<td></td>
			<td></td>
			<td>
				<a class="editable-submit" onclick="save_disciplinadocente(<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['id']; ?>);" href="#" ><i class="icon-ok-sign"></i> Salva</a> 
				<a class="editable-cancel" onclick="remove_disciplinadocente(<?php echo $disciplinaDocente['ClasseDisciplinaDocente']['id']; ?>);" href="#" ><i class="icon-remove-sign"></i> Rimuovi</a> 
			</td>	
		</tr>
		<?php }?>
		</table>
		</form>
		<script>
		
		function save_disciplinadocente(idrow){
			postRow = {
				'id' : $('#'+idrow ).find('#idClasseDisciplina').val(), 
				'prove' : $('#'+idrow ).find('#prove').attr('data-value'), 
				'oresett' : $('#'+idrow ).find('#oreSettimanali').val(), 
				'docente' : $('#'+idrow ).find('#docenti').attr('data-value') 
				
			};
			console.log(postRow);	
			var jqxhr = $.post("<?php echo $this->webroot; ?>Classe/save_disciplinadocente", postRow ,function(data) {
			  setFlash('Disciplina Associata con successo.');
			})
			.fail(function(jqXHR, textStatus) {
				  alert( "Request failed: " + textStatus );
				})
		}
		
		
		function remove_disciplinadocente(idrow){
			postRow = {
				'id' : $('#'+idrow ).find('#idClasseDisciplina').val() 
			};
			var jqxhr = $.post("<?php echo $this->webroot; ?>Classe/remove_disciplinadocente", postRow ,function(data) {
			  setFlash(data.msg);
			  console.log(data.msg);
			  $('#'+idrow ).remove();
			})
			.fail(function(jqXHR, textStatus) {
				  alert( "Request failed: " + textStatus );
				})
		}
		
		
		
		
		$(function(){
			
			$('#sticky').waypoint('sticky', {
			  stuckClass: 'span12 stuck',
			  offset: 50 // Apply "stuck" when element 30px from top
			});
			
			//$('#MySpinner').spinner();
			$(".select2").select2();
			
		
			//CAMPI EDITABILI 
			 $('.optionsProve').editable({
		        emptytext : 'da associare',    
		        source: [
		              {value: 1, text: 'Scritto'},
		              {value: 2, text: 'Orale'},
		              {value: 3, text: 'Pratica'},
		              {value: 4, text: 'Altro'}
		           ],
   				 //pk: 1,
				 title: 'Tipologie di Prova',
				 send:'never'
		    });
			
			$('.optionsProve').editable('option', 'validate', function(v) {
				$(this).attr('data-value',v);
			});
			
			
			
		    $('.change_docente').editable({
		    	emptytext : 'da associare',
		        source: [
		              {id: '0' , text: ''},
		        <?php foreach($classe["Docente"] as $docente) {?>
		              {id: '<?php echo $docente['id']?>' , text: '<?php echo $docente['User']['Anagrafica']['cognome']?> <?php echo $docente['User']['Anagrafica']['nome']?>'},
		        <?php } ?>
		           ],
		        select2: {
		           multiple: false
		        },
		         type: 'text',
				 //pk: 1,
				 title: 'Docenti della materia'
		    });
		    
		    $('.change_docente').editable('option', 'validate', function(v) {
			    $(this).attr('data-value',v);
			});
		    
		    $('#anno').editable({
                type: 'text',
                //pk: 1,
                url: '/post',
                title: 'Modifica la classe',
                placement:'bottom',
            });
		    $('#sezione').editable({
                type: 'text',
                //pk: 1,
                url: '/post',
                title: 'Modifica la sezione',
                placement:'bottom',
            });
            $('#indirizzostudio').editable({
                value: <?php echo $classe['IndirizzoStudio']['id']; ?>,
                //pk: 1,
                url: '/post',
                title: 'Modifica l\'indirizzo di Studio',
                placement:'bottom', 
                select2: {
                   multiple: false
                },   
                type: 'select',
                source: [
                       <?php foreach($listIndirizziStudio as $indirizzoStudio){?>
                      {value: <?php echo $indirizzoStudio['IndirizzoStudio']['id']; ?>, text: '<?php echo $indirizzoStudio['IndirizzoStudio']['denominazione']; ?>'},
                      <?php }?>
                   ]
    });
            
		    
		    // FINE CAMPI EDITABILI
		});
		</script>
	</div>
</div>

<?php //print_r($classeDisciplinaDocenteList);?>